define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'express/batch/index' + location.search,
                    add_url: 'express/batch/add',
                    edit_url: 'express/batch/edit',
                    del_url: 'express/batch/del',
                    multi_url: 'express/batch/multi',
                    import_url: 'express/batch/import',
                    table: 'express_batch',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'batch_no', title: __('batch_no')},
                        {field: 'number', title: __('Number')},
                        {field: 'state', title: __('State'), searchList: {"1":__('State 1'),"2":__('State 2'),"3":__('State 3'),"4":__('State 4'),"5":__('State 5'),"6":__('State 6'),"7":__('State 7'),"8":__('State 8'),"9":__('State 9')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                /*{
                                    name: 'export',
                                    text: __('导出批次'),
                                    title: __('导出批次'),
                                    classname: 'btn btn-xs btn-info',
                                    icon: 'glyphicon glyphicon-export icon-share',
                                    url: 'express/batch/export',
                                    extend: ' target="_blank"'
                                },
                                {
                                    name: 'scanning',
                                    text: __('扫描包裹'),
                                    title: __('扫描包裹'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-barcode',
                                    url: 'express/batch/scanning',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    },

                                },*/
                                {
                                    name: 'package',
                                    text: __('包裹列表'),
                                    title: __('包裹列表'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-gift',
                                    url: 'express/package/index',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    },
                                    extend: "data-area='[\"90%\",\"90%\"]'"
                                },
                               /* {
                                    name: 'download',
                                    text: __('下载身份证'),
                                    title: __('下载身份证'),
                                    classname: 'btn btn-xs btn-success',
                                    icon: 'fa fa-download',
                                    url: 'express/batch/download',
                                    extend: ' target="_blank"'
                                },*/
                                {
                                    name: 'setState',
                                    text: __('改变状态'),
                                    title: __('改变状态'),
                                    classname: 'btn btn-xs btn-warning btn-setState btn-click',
                                    icon: 'fa fa-cog',
                                    click:function (row) {
                                        var data = Table.api.getrowdata(table,row.rowIndex);
                                        console.log(data.id)
                                        setTimeout(function () {
                                            $('#c-ids').val(data.id);
                                        },500);
                                    }
                                },

                            ],events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $(document).on("click", ".btn-setState", function () {
                var that = this;
                var id = "setBatchTpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: "设置状态",
                    area: ["500px", "300px"],
                    content: content,
                    success: function (layero) {
                        var form = $("form", layero);
                        var ids = Table.api.selectedids(table);
                        $('#c-ids').val(ids.join(','));
                        Form.api.bindevent(form, function (data) {
                            $(".btn-refresh").trigger("click");
                            Layer.closeAll();
                        });
                    }
                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        scanning:function(){
            Form.api.bindevent($("form[role=form]"),function (ret,data) {
                Toastr.success('添加成功');
                $('#c-express_no').val('');
                $('#c-express_no').focus();
                return false;
            },function () {

            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
